package quotation.config;


import cn.hutool.core.util.StrUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import quotation.utils.StringToDateUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 配置类
 *
 * @author cjw
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/",
                        "/favicon.ico",
                        "/swagger**/**",
                        "/error",
                        "/v2/**",
                        "/images/**",
                        "/**.html"
                );
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE", "PATCH")
                .allowedHeaders("Origin", "X-Requested-With", "token", "Content-Type", "Accept", "x-auth-token")
                .maxAge(3600);
    }



    @Override
    public void addFormatters(FormatterRegistry registry) {

        registry.addConverter(new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                if (source == null || "".equals(source.trim())) {
                    return null;
                }
                return LocalDate.parse(source);
            }
        });

        registry.addConverter(new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String source) {
                if (source == null || "".equals(source.trim())) {
                    return null;
                }
                return LocalDateTime.parse(source.trim(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
        });
        registry.addConverter(new Converter<String, String>() {
            @Override
            public String convert(String source) {
                if (source == null) {
                    return null;
                }
                return source.trim();
            }
        });

        registry.addConverter(new Converter<String, Date>() {
            @Override
            public Date convert(String source) {
                if (StrUtil.isEmpty(source) || "".equals(source.trim())) {
                    return null;
                }
                return StringToDateUtil.convert(source);
            }
        });
    }

    @Bean
    public AntPathMatcher antPathMatcher(){
        return new AntPathMatcher();
    }


}
